package com.monkeygroup.mygoplus.view.home.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.http.ThingsHttpModel;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 取樣物件
 * Created by cumt on 2017/5/27.
 */

public class PublishedMineActivity extends BaseActivity implements View.OnClickListener{
    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PublishedMineActivity.class);

        activity.startActivity(intent);
    }

    private ImageView imgBack;
    private TextView txtTitle;

    private LinearLayout ll_rent,ll_all,ll_sell;
    private TextView txt_rent,txt_all,txt_sell;

    private LinearLayout  default_page,ll_data;

    private PersonalNomalInfo user;

    //請求
    private ThingsHttpModel httpThingsModel;
    private Map<String, String> params;

    private PullToRefreshListView lv_result;

    private List<RentVO> sellList;
    private SellLeftAdapter adapter;
    //出租
    private String type="1";

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.published_modify);
    }

    @Override
    public void initView() {

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("選擇複製物件");

        ll_rent= (LinearLayout) findViewById(R.id.ll_rent);
        txt_rent= (TextView) findViewById(R.id.txt_rent);
        ll_all= (LinearLayout) findViewById(R.id.ll_all);
        txt_all= (TextView) findViewById(R.id.txt_all);
        ll_sell= (LinearLayout) findViewById(R.id.ll_sell);
        txt_sell= (TextView) findViewById(R.id.txt_sell);

        default_page= (LinearLayout) findViewById(R.id.default_page);
        ll_data= (LinearLayout) findViewById(R.id.default_page);

        lv_result = (PullToRefreshListView) findViewById(R.id.lv_result);
        sellList = new ArrayList<>();
         adapter =  new SellLeftAdapter(PublishedMineActivity.this);
        lv_result.setAdapter(adapter);

        params = new HashMap<>();
        user = LocalInfo.getUserInfo(PublishedMineActivity.this);
        Log.e("user.getSeq()===",""+user.getSeq());
        params.put("user",""+user.getSeq());
        //物件状态(0:下架；1：上架；2：已成交)
        params.put("acti","1");
        //物件类型(buy：出售；rent：出租；assign：顶让；all：不限)
        params.put("type","rent");
        params.put("page","0");
        params.put("pagesize","10");
        params.put("lang","zh_TW");

        initRequest();
        if(user.getSeq()!=0){
            httpThingsModel.requestThings();
        }

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_rent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type="1";
                params.put("type","rent");
                txt_rent.setTextColor(getResources().getColor(R.color.base_color));
                txt_all.setTextColor(getResources().getColor(R.color.bottom_gray));
                txt_sell.setTextColor(getResources().getColor(R.color.bottom_gray));
                httpThingsModel.requestThings();
            }
        });
        ll_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type="2";
                params.put("type","all");
                txt_rent.setTextColor(getResources().getColor(R.color.bottom_gray));
                txt_all.setTextColor(getResources().getColor(R.color.base_color));
                txt_sell.setTextColor(getResources().getColor(R.color.bottom_gray));
                httpThingsModel.requestThings();
            }
        });

        ll_sell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type="3";
                params.put("type","buy");
                txt_rent.setTextColor(getResources().getColor(R.color.bottom_gray));
                txt_all.setTextColor(getResources().getColor(R.color.bottom_gray));
                txt_sell.setTextColor(getResources().getColor(R.color.base_color));
                httpThingsModel.requestThings();
            }
        });
    }


    private void initRequest(){

        httpThingsModel = new ThingsHttpModel(RequestUrl.getmyproperty,params,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {
                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {

                    }

                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {
                        Gson gson = new Gson();
//                        if(VilidateUtil.isSuccess(Integer.parseInt(resultCode))){

                            if("ERROR_OK".equals(resultCode)){
                                List<RentVO> propertys = gson.fromJson(gson.toJson(property), new TypeToken<List<RentVO>>() {
                                }.getType());

                                if(propertys==null || propertys.size()==0){
                                    default_page.setVisibility(View.VISIBLE);
                                    ll_data.setVisibility(View.GONE);
                                }
                                else{
                                    default_page.setVisibility(View.GONE);
                                    ll_data.setVisibility(View.INVISIBLE);
                                    sellList.clear();
                                    sellList.addAll(propertys);
                                    adapter.notifyDataSetChanged();
                                }

                            }


//                        }

                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {

                    }
                }, true);

    }


    public class SellLeftAdapter extends BaseAdapter {

        private LayoutInflater inflater;

        public SellLeftAdapter(Context context){
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return sellList.size();
        }

        @Override
        public Object getItem(int position) {
            return sellList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            convertView = inflater.inflate(R.layout.mine_property_item, null);
            final RentVO obj = sellList.get(position);
            final SimpleDraweeView txtName = (SimpleDraweeView) convertView
                    .findViewById(R.id.img_sell_house_item);
            final TextView txt_sell_house_item_name = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_name);
            final TextView txt_sell_house_item_price = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_price);
            final TextView txt_sell_house_item_address = (TextView) convertView
                    .findViewById(R.id.txt_sell_house_item_address);


            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            Images.with(PublishedMineActivity.this).load(Uri.parse(obj.getImg())).resize(130,90).into(txtName);
            txt_sell_house_item_name.setText(obj.getName());
            txt_sell_house_item_price.setText(String.format("坪数：%s",obj.getLawnarea())+"坪");
            txt_sell_house_item_address.setText(String.format("售价：%s",""+obj.getSaleprice()));
            return convertView;
        }



    }










}


